package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import util.JDBCHelper;

public class FlightIO {

	/**
	 * Check if the number of seats requested are available. This takes the
	 * classType and the number of seat request and compares it with the 
	 * capacity on the plane.
	 * @param seats
	 * @param planeID
	 * @param reservedSeats
	 * @param classType
	 * @return boolean if the seats are available
	 */
	public boolean checkSeats(int seats, int planeID, int reservedSeats, String classType){
		JDBCHelper jdbc= new JDBCHelper("cse.unl.edu","cse464" ,"mrogers", "3ZF:ey"); 
		ArrayList<Object> param =  new ArrayList<Object>();
		param.add(planeID);
		ResultSet rs = jdbc.queryDB("SELECT " + classType + "_capacity from planes WHERE id=?", param);
		int capacitySeats = 0;
		try {
			if (rs != null){
				try {
					while(rs.next()){
						capacitySeats=rs.getInt(classType + "_capacity"); 
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			jdbc.conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(reservedSeats + seats > capacitySeats){
			return false; 
		} else {
			return true;
		}
	}

	/**
	 * Method to get the flight based on the given flight id. 
	 * @param flightID
	 * @return Flight
	 */
	public Flight getFlight(int flightID){
		JDBCHelper jdbc= new JDBCHelper("cse.unl.edu","cse464" ,"mrogers", "3ZF:ey"); 
		ArrayList<Object> param =  new ArrayList<Object>();
		param.add(flightID);
		ResultSet rs = jdbc.queryDB("SELECT * from flights WHERE id=?", param);
		Flight flight = null;
		try {
			if (rs != null){
				try {
					while(rs.next()){
						int id =rs.getInt("id"); 
						int planeID =rs.getInt("plane"); 
						String operator =rs.getString("operator"); 
						String source =rs.getString("source"); 
						String destination =rs.getString("destination"); 
						int firstClass =rs.getInt("first_class_reserved");
						int business =rs.getInt("business_reserved"); 
						int economy =rs.getInt("economy_reserved"); 
						Timestamp departure = rs.getTimestamp("departure");
						Timestamp arrival = rs.getTimestamp("arrival");
						String timeDifference = getTimeDiff(departure, arrival);
						int cost = getCost(id);
						flight = new Flight(id, planeID, operator, source, 
								destination, firstClass, business, economy, 
								departure, arrival, timeDifference, cost);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}			
			jdbc.conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return flight;
	}

	/**
	 * Method to get an ArrayList of Flights based on the search criteria. The 
	 * returned flights need to match the source, destiantion, date, class 
	 * type, and number of seats.
	 * @param sourceIn
	 * @param destinationIn
	 * @param departureIn
	 * @param classType
	 * @param seats
	 * @return ArrayList of Flights
	 */
	public ArrayList<Flight> searchFlights(String sourceIn, String destinationIn,
			String departureIn, String classType, int seats){
		JDBCHelper jdbc= new JDBCHelper("cse.unl.edu","cse464" ,"mrogers", "3ZF:ey"); 
		SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy");
		
		ArrayList<Object> param =  new ArrayList<Object>();
		param.add(sourceIn);
		param.add(destinationIn);
		ResultSet rs = jdbc.queryDB("SELECT * from flights WHERE source=? and destination=?", param);
		ArrayList<Flight> flights = new ArrayList<Flight>();
		Flight flight = null;
		try {
			if (rs != null){
				while(rs.next()){
					Timestamp departure = rs.getTimestamp("departure");
					boolean sameDate = departureIn.equals(fmt.format(departure));
					if(sameDate){
						int id =rs.getInt("id"); 
						int planeID =rs.getInt("plane");
						boolean availableSeats = false;
						int reservedSeats = rs.getInt(classType + "_reserved");
						availableSeats = checkSeats(seats, planeID, reservedSeats, classType);
						if(availableSeats){
							String operator =rs.getString("operator"); 
							String source =rs.getString("source"); 
							String destination =rs.getString("destination"); 
							int firstClass =rs.getInt("first_class_reserved");
							int business =rs.getInt("business_reserved"); 
							int economy =rs.getInt("economy_reserved"); 
							
							Timestamp arrival = rs.getTimestamp("arrival");
							String timeDifference = getTimeDiff(departure, arrival);
							int cost = getCost(id);

							flight = new Flight(id, planeID, operator, source, 
									destination, firstClass, business, economy, 
									departure, arrival, timeDifference, cost);
							flights.add(flight);
						}
					}
				}
			}			
			jdbc.conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}


		return flights;
	}

	/**
	 * Method to get the cost. This is done to generate a few different amounts
	 * @param id
	 * @return int cost
	 */
	private int getCost(int id){
		int cost = 0;
		int modID = id%3;
		if(modID == 0){
			cost = 210;
		} else if(modID == 1){
			cost = 240;
		} else if(modID == 2){
			cost = 270;
		} 
		return cost;
	}
	
	/**
	 * Method to take two time stamps and find the time difference in hours and
	 * and minutes
	 * @param t1
	 * @param t2
	 * @return String time
	 */
	private String getTimeDiff(Timestamp t1, Timestamp t2){
		long diff = t2.getTime() - t1.getTime();

		long diffMinutes = diff / (60 * 1000) % 60;
		long diffHours = diff / (60 * 60 * 1000) % 24;
		String mins;
		String hrs;

		if(diffMinutes != 1){
			mins = diffMinutes + " minutes";
		} else {
			mins = diffMinutes + " minute";
		}

		if(diffHours != 1){
			hrs = diffHours + " hours";
		} else {
			hrs = diffHours + " hour";
		}

		String difference = hrs + " " + mins;
		return difference;
	}
}
